package com.p2p.backend.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.backend.service.AccountInternalService;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.constents.InnerAccountConsts;
import com.p2p.base.model.Money;

/**
 * leiwen@yiqihao.com
 */
@Controller
@RequestMapping("accountinternal")
public class AccountInternalController {

	@Resource
	private AccountInternalService accountInternalService;
	
	@PermissionNeed("accountinternal_list")
	@RequestMapping("list")
	public void list(Model model){
		List<Map<String,Object>> list=accountInternalService.list();
		//Map<String,Map<String,Object>> map = accountInternalService.listMap();
		model.addAttribute("res", 1);
		Map<String,Object> data = new HashMap<String,Object>();
		model.addAttribute("data", data);
		data.put("rows", list);
		data.put("total", list.size());
		
		Money allBalance = new Money(0,0);
		for(Map<String, Object> temp:list){
			if(StringUtils.equals(MapUtils.getString(temp, "accountNo"), InnerAccountConsts.innerTotalAccountNo)){
				Money tempAmount = (Money)temp.get("balance");
				allBalance.add(tempAmount);
			}else{
				Money tempAmount = (Money)temp.get("balance");
				allBalance.sub(tempAmount);
			}
		}
		
	
		List<Map<String,Object>> footer = new ArrayList<Map<String,Object>>();
		data.put("footer", footer);
		Map<String,Object> footerOne =new HashMap<String,Object>();
		footer.add(footerOne);
		footerOne.put("desc", "用户总余额");
		footerOne.put("balance", allBalance);
	}
}
